Telegram Group & Telegram Channel
🛠 How to: как соблюдать принцип DRY

DRY (Don’t Repeat Yourself) — принцип, согласно которому каждый фрагмент знания должен существовать в системе только в одном месте. Никакого копипаста и дублирующей логики.

Проблема: дублирование валидации
// Пример плохого кода
if (user.Age < 18)
throw new Exception("User must be at least 18");

...

if (user.Age < 18)
return BadRequest("User must be at least 18");

При изменении правила — нужно помнить обновить его везде. Ловушка копипаста.

Решение: вынос логики в общее правило
public static class ValidationRules
{
public static bool IsAdult(User user) => user.Age >= 18;
}


Теперь в коде
if (!ValidationRules.IsAdult(user))
throw new Exception("User must be at least 18");

// И в другом месте:
if (!ValidationRules.IsAdult(user))
return BadRequest("User must be at least 18");


Альтернатива: использование FluentValidation или DataAnnotations
public class User
{
[Range(18, int.MaxValue, ErrorMessage = "User must be at least 18")]
public int Age { get; set; }
}


Где чаще всего нарушают DRY в .NET:


• Повторяющиеся SQL-запросы и фильтры
• Повторение одинаковых exception'ов, логов, сообщений
• Дублирование конфигурации
• UI-формы и компоненты

🐸Библиотека шарписта #буст
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/csharpproglib/5841
Create:
Last Update:

🛠 How to: как соблюдать принцип DRY

DRY (Don’t Repeat Yourself) — принцип, согласно которому каждый фрагмент знания должен существовать в системе только в одном месте. Никакого копипаста и дублирующей логики.

Проблема: дублирование валидации

// Пример плохого кода
if (user.Age < 18)
throw new Exception("User must be at least 18");

...

if (user.Age < 18)
return BadRequest("User must be at least 18");

При изменении правила — нужно помнить обновить его везде. Ловушка копипаста.

Решение: вынос логики в общее правило
public static class ValidationRules
{
public static bool IsAdult(User user) => user.Age >= 18;
}


Теперь в коде
if (!ValidationRules.IsAdult(user))
throw new Exception("User must be at least 18");

// И в другом месте:
if (!ValidationRules.IsAdult(user))
return BadRequest("User must be at least 18");


Альтернатива: использование FluentValidation или DataAnnotations
public class User
{
[Range(18, int.MaxValue, ErrorMessage = "User must be at least 18")]
public int Age { get; set; }
}


Где чаще всего нарушают DRY в .NET:


• Повторяющиеся SQL-запросы и фильтры
• Повторение одинаковых exception'ов, логов, сообщений
• Дублирование конфигурации
• UI-формы и компоненты

🐸Библиотека шарписта #буст

BY Библиотека шарписта | C#, F#, .NET, ASP.NET




Share with your friend now:
tg-me.com/csharpproglib/5841

View MORE
Open in Telegram


Библиотека шарписта | C F NET ASP NET Telegram | DID YOU KNOW?

Date: |

Look for Channels Online

You guessed it – the internet is your friend. A good place to start looking for Telegram channels is Reddit. This is one of the biggest sites on the internet, with millions of communities, including those from Telegram.Then, you can search one of the many dedicated websites for Telegram channel searching. One of them is telegram-group.com. This website has many categories and a really simple user interface. Another great site is telegram channels.me. It has even more channels than the previous one, and an even better user experience.These are just some of the many available websites. You can look them up online if you’re not satisfied with these two. All of these sites list only public channels. If you want to join a private channel, you’ll have to ask one of its members to invite you.

The global forecast for the Asian markets is murky following recent volatility, with crude oil prices providing support in what has been an otherwise tough month. The European markets were down and the U.S. bourses were mixed and flat and the Asian markets figure to split the difference.The TSE finished modestly lower on Friday following losses from the financial shares and property stocks.For the day, the index sank 15.09 points or 0.49 percent to finish at 3,061.35 after trading between 3,057.84 and 3,089.78. Volume was 1.39 billion shares worth 1.30 billion Singapore dollars. There were 285 decliners and 184 gainers.

Библиотека шарписта | C F NET ASP NET from tw


Telegram Библиотека шарписта | C#, F#, .NET, ASP.NET
FROM USA